package net.sxnic.normal.security.action;

import net.sxinfo.core.util.CookieUtils;
import net.sxnic.ugr.UgrActionSupport;
import net.sxnic.ugr.UgrConstant;
import net.sxnic.ugr.context.AppContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 
 * Introduction：
 * 
 * @author 孙宇飞 create date : 2010-5-16
 */
@Controller
public class Logout extends UgrActionSupport {

	private static Logger logger = LoggerFactory.getLogger(Logout.class);

	@RequestMapping(value = "/logout.jspa" ,method = {RequestMethod.POST,RequestMethod.GET})
	public String execute() throws Exception {

		if (AppContext.getUserName(request) == null) {
			return "notAuthorized";
		}

		// log
		logger.debug("===" + AppContext.getUserName(request)
				+ "===logout===success===");

		if (UgrConstant.LOGIN_USER_MAP == null) {
			return SUCCESS;
		}

		// 从在线用户列表中删除
		if (UgrConstant.LOGIN_USER_MAP.containsKey(AppContext
				.getUserName(request))) {
			UgrConstant.LOGIN_USER_MAP.remove(AppContext.getUserName(request));
		}

		request.getSession().invalidate();

		// 删除cookie
		if (CookieUtils.getCookie(request, UgrConstant.AUTOLOGIN_COOKIE_KEY) != null) {
			logger.debug("清理AutoLogin Cookie");
			CookieUtils.invalidateCookie(response,
					UgrConstant.AUTOLOGIN_COOKIE_KEY);
		}

		return SUCCESS;
	}
}
